﻿Imports System.Data.SqlClient
Imports C1.Win.C1List.C1Combo


Public Class Frm_PhieuNhap
    Public clsdm As New Cls_DanhMuc

    Private Sub Frm_PhieuNhap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        clsdm.FillC1ComBo(Me.cbxtennv, "select * from V_NhanVien", "Tennv", "Manv")
        clsdm.FillC1ComBo(Me.cbxnhacc, "select * from NhaCungCap", "Tenncc", "Mancc")
        clsdm.FillC1ComBo(Me.cbxtensp, "Select MaSP,TenSP from SanPham", "TenSp", "MaSP")
        FillData()
        an()
    End Sub
    Sub an()
        Me.cbxtensp.Enabled = False
        Me.txtsoluong.Enabled = False
        Me.cbxdvt.Enabled = False
        Me.txtdongia.Enabled = False
        Me.txtthanhtien.Enabled = False
        Me.cbxtennv.Enabled = False
        Me.cbxnhacc.Enabled = False
        Me.cbxtensp.Text = "       --- Chọn --- "
        Me.cbxtennv.Text = "       --- Chọn --- "
        Me.cbxnhacc.Text = "       --- Chọn --- "
        Me.cbxdvt.Text = "    --- Chọn --- "
    End Sub
    Sub Mo()
        Me.cbxtensp.Enabled = True
        Me.txtsoluong.Enabled = True
        Me.cbxdvt.Enabled = True
        Me.txtdongia.Enabled = True
        Me.txtthanhtien.Enabled = True

    End Sub
    Private Sub FillData()
        clsdm.FillDataGirdView("select * from CTPhieuNhap a,SanPham b where a.masp=b.masp and mapn='" & Me.txtmapn.Text & "'", Me.dtg, Me.dts)
    End Sub
#Region " Thêm xóa sửa"
    Private Sub btthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btthem.Click
        If Me.btthem.Text = "&Thêm (F2)" Then
            Me.btthem.Text = "&Hủy"
            Me.btluu.Enabled = True
            Me.btsua.Enabled = False
            Me.btxoa.Enabled = False
            Me.cbxtennv.Enabled = True
            Me.cbxtennv.Focus()
            Me.cbxnhacc.Enabled = True
            clsdm.FillDataText(Me.txtmapn, "Tang_MaPN")

            clsdm.FillDataGirdView("select * from CTPhieuNhap a,SanPham b where a.masp=b.masp and mapn='" & Me.txtmapn.Text & "'", Me.dtg, Me.dts)
        Else
            Me.btthem.Text = "&Thêm (F2)"
            Me.txtmapn.Text = ""
            Me.btsua.Enabled = True
            Me.btxoa.Enabled = True
            an()
        End If
        Me.txttongsl.Text = 0
        Me.txttongtien.Text = 0
    End Sub
    Private Sub btluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btluu.Click
        If Me.btluu.Text = "&Thêm Số Phiếu (F3)" Then
            Me.btluu.Text = "&Cập Nhật Chi Tiết (F3)"
            Dim mycmd As New SqlCommand("ThemPN", con)
            mycmd.CommandType = CommandType.StoredProcedure
            mycmd.Parameters.Add("@mapn", SqlDbType.NVarChar).Value = txtmapn.Text.Trim
            mycmd.Parameters.Add("@Manv", SqlDbType.Int).Value = cbxtennv.SelectedValue
            mycmd.Parameters.Add("@Mancc", SqlDbType.Int).Value = cbxnhacc.SelectedValue
            mycmd.Parameters.Add("@Ngaynhap", SqlDbType.DateTime).Value = dtp_ngay.Text
            mycmd.Parameters.Add("@Lydo", SqlDbType.NVarChar).Value = txtlydo.Text
            mycmd.Parameters.Add("@TongSL", SqlDbType.Int).Value = txttongsl.Text
            mycmd.Parameters.Add("@TongTien", SqlDbType.Int).Value = txttongtien.Text
            Try
                mycmd.ExecuteNonQuery()
                Mo()
            Catch ex As Exception
                MsgBox("Lỗi Cập Nhật " & ex.Message, MsgBoxStyle.Critical, "Thông Báo")
            End Try
            Me.btthem.Text = "&Thêm (F2)"
            Me.cbxtensp.Focus()
        Else
            Me.btluu.Text = "&Cập Nhật Chi Tiết (F3)"
            Me.btsua.Enabled = True
            Me.btxoa.Enabled = True

            Dim mycmd As New SqlCommand("ThemCTPN", con)
            mycmd.CommandType = CommandType.StoredProcedure
            mycmd.Parameters.Add("@mapn", SqlDbType.NVarChar).Value = txtmapn.Text
            mycmd.Parameters.Add("@Masp", SqlDbType.NVarChar).Value = cbxtensp.SelectedValue
            mycmd.Parameters.Add("@Soluong", SqlDbType.Int).Value = txtsoluong.Text
            mycmd.Parameters.Add("@DVT", SqlDbType.NVarChar).Value = cbxdvt.Text
            mycmd.Parameters.Add("@Dongia", SqlDbType.Money).Value = Format(CInt(txtdongia.Text))
            mycmd.Parameters.Add("@Thanhtien", SqlDbType.Money).Value = txtthanhtien.Text
            Try
                mycmd.ExecuteNonQuery()
                Me.cbxtensp.Focus()
                clsdm.FillDataText(Me.txttongsl, "select TongSl from PhieuNhap where mapn='" & Me.txtmapn.Text.Trim & "'")
                clsdm.FillDataText(Me.txttongtien, "select TongTien from PhieuNhap where mapn='" & Me.txtmapn.Text.Trim & "'")
            Catch ex As Exception
                MsgBox("Lỗi Cập Nhật " & ex.Message, MsgBoxStyle.Critical, "Thông Báo")
            End Try
            FillData()

        End If
    End Sub
    Private Sub btxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btxoa.Click
        Dim strma, strten As String
        strma = dtg.CurrentRow.Cells(0).Value.ToString()
        strten = dtg.CurrentRow.Cells(1).Value.ToString()
        If MsgBox("Bạn có chắc xóa : " & strma & "Có tên: " & strten, MsgBoxStyle.YesNo, "Thông Báo") = MsgBoxResult.Yes Then
            Try
                Dim myCmd As New SqlCommand("XoaCTPN", con)
                myCmd.CommandType = CommandType.StoredProcedure
                myCmd.Parameters.Add("@mapn", SqlDbType.NVarChar).Value = Me.txtmapn.Text
                myCmd.Parameters.Add("@masp", SqlDbType.NVarChar).Value = Me.cbxtensp.SelectedValue
                myCmd.ExecuteNonQuery()
                FillData()
            Catch ex As Exception
                MsgBox("Lỗi khi xóa" & ex.Message)
                Exit Sub
            End Try
        End If
        clsdm.FillDataText(Me.txttongsl, "select TongSl from PhieuNhap where mapn='" & Me.txtmapn.Text.Trim & "'")
        clsdm.FillDataText(Me.txttongtien, "select TongTien from PhieuNhap where mapn='" & Me.txtmapn.Text.Trim & "'")
    End Sub
    Private Sub btsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btsua.Click
        Dim mycmd As New SqlCommand("SuaCTPN", con)
        mycmd.CommandType = CommandType.StoredProcedure
        mycmd.Parameters.Add("@mapn", SqlDbType.NVarChar).Value = txtmapn.Text
        mycmd.Parameters.Add("@Masp", SqlDbType.NVarChar).Value = cbxtensp.SelectedValue
        mycmd.Parameters.Add("@Soluong", SqlDbType.Int).Value = txtsoluong.Text
        mycmd.Parameters.Add("@DVT", SqlDbType.NVarChar).Value = cbxdvt.Text
        mycmd.Parameters.Add("@Dongia", SqlDbType.Money).Value = CInt(txtdongia.Text)
        mycmd.Parameters.Add("@Thanhtien", SqlDbType.Money).Value = CInt(txtthanhtien.Text)
        Try
            mycmd.ExecuteNonQuery()
            FillData()
            clsdm.FillDataText(Me.txttongsl, "select TongSl from PhieuNhap where mapn='" & Me.txtmapn.Text.Trim & "'")
            clsdm.FillDataText(Me.txttongtien, "select TongTien from PhieuNhap where mapn='" & Me.txtmapn.Text.Trim & "'")
        Catch ex As Exception
            MsgBox("Lỗi Cập Nhật " & ex.Message, MsgBoxStyle.Critical, "Thông Báo")
        End Try

    End Sub
    Private Sub btthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btthoat.Click
        Me.Close()
    End Sub
#End Region

    Private Sub txtthanhtien_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtthanhtien.TextChanged
        If IsNumeric(Me.txtthanhtien.Text.Trim) = True And Len(Me.txtthanhtien.Text) > 3 Then
            Me.txtthanhtien.Text = Format(CInt(Me.txtthanhtien.Text), "#,###")
        End If
    End Sub
    Private Sub cbxtensp_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxtensp.SelectedValueChanged
        clsdm.FillDataText(Me.txtdongia, "select dongia from SanPham where masp='" & Me.cbxtensp.SelectedValue & "'")
        clsdm.FillCombo(Me.cbxdvt, "select * from SanPham a,donvitinh b where a.madvt=b.madvt and tensp='" & Me.cbxtensp.Text & "'", "Tendvt", "Madvt")
    End Sub

    ' Khi nhấn phím tab,Enter để tính cột thành tiền
    Private Sub txttongtien_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txttongtien.TextChanged
        If IsNumeric(Me.txttongtien.Text.Trim) = True And Len(Me.txttongtien.Text) > 3 Then
            Me.txttongtien.Text = Format(CInt(Me.txttongtien.Text), "#,###")
        End If
    End Sub
    Private Sub txtsoluong_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtsoluong.TextChanged
        If Me.txtsoluong.Text <> "" And IsNumeric(Me.txtsoluong.Text) Then
            Me.txtthanhtien.Text = Me.txtdongia.Text * Me.txtsoluong.Text
        End If
    End Sub
    Private Sub txtdongia_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtdongia.TextChanged
        If IsNumeric(Me.txtdongia.Text.Trim) = True And Len(Me.txtdongia.Text) > 3 Then
            Me.txtdongia.Text = Format(CInt(Me.txtdongia.Text), "#,###")
        End If
    End Sub
    Private Sub txtthanhtien_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtthanhtien.GotFocus
        If Me.txtsoluong.Text <> "" And IsNumeric(Me.txtsoluong.Text) Then
            Me.txtthanhtien.Text = Me.txtdongia.Text * Me.txtsoluong.Text
        End If
        If IsNumeric(Me.txtthanhtien.Text.Trim) = True And Len(Me.txtthanhtien.Text) > 3 Then
            Me.txtthanhtien.Text = Format(CInt(Me.txtthanhtien.Text), "#,###")
        End If
    End Sub

    Private Sub cbxdvt_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxdvt.GotFocus
        clsdm.FillCombo(Me.cbxdvt, "select * from DonViTinh", "TenDVT", "Madvt")
    End Sub

  
End Class